﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;
public enum ButtonState
{
    JiBenState, //选中基本信息
    RuHuiState, //选中入会申请
    KaiFangState,//选中开设包房
}
public class ClubhuizhangModel : MonoBehaviour
{

    EginUser user = EginUser.Instance;
    public ButtonState _currentState = ButtonState.JiBenState;

    public UILabel _name;
    public UILabel _id;
    public UILabel _clubNum;
    public GameObject CloseBtn;
    public GameObject changeNameBtn;
    public GameObject moreBtn;
    public GameObject mmoreBtn;

    public GameObject jiben_an_btn;
    public GameObject ruhuisend_an_btn;
    public GameObject kaifang_an_btn;

    public GameObject jibenxinxi_ui;
    public GameObject ruhuisend_ui;
    public GameObject kaifang_ui;

    //基本信息
    public Transform grid_jibenxinxi;
    public GameObject chird_jibenxinxi;

    //入会申请
    public Transform grid_ruhuisheng;
    public GameObject chird_ruhuisheng;

    //开设包房
    public Transform grid_kaifang;
    public GameObject chird_kaifang;
    public GameObject KaiFangJieShao;
    private List<GameObject> dizhu_sprites = new List<GameObject>(); //将所有的底注图片放到列表里（方便替换）

    public int current_dizhu = 0; //当前选中底注
    public List<int> current_beishu = new List<int>(); //当前选中倍数(多选)
    public int _isNullRoom = 0; //1是空 2是非空
    public GameObject CreateRoomBtn;

    public int _page = 1;//当前基本信息页面；

    public int _room_page = 0;//当前俱乐部房间页面

    public GameObject HaveBtn;
    public GameObject NULLbtn;
    private int allnum = 0;//基本信息人个数
    private static ClubhuizhangModel _instance;

    public static ClubhuizhangModel Instance
    {
        get
        {
            return _instance;
        }

    }

    void Awake()
    {
        _instance = this;
        //left按键
        UIEventListener.Get(jiben_an_btn).onClick = ButtonClick;
        UIEventListener.Get(ruhuisend_an_btn).onClick = ButtonClick;
        UIEventListener.Get(kaifang_an_btn).onClick = ButtonClick;

        //常规按键
        UIEventListener.Get(CloseBtn).onClick = ButtonClick;
        UIEventListener.Get(changeNameBtn).onClick = ButtonClick;

        UIEventListener.Get(moreBtn).onClick = ButtonClick;
        UIEventListener.Get(mmoreBtn).onClick = ButtonClick;
        UIEventListener.Get(CreateRoomBtn).onClick = ButtonClick;

        chird_jibenxinxi.SetActive(false);
        chird_ruhuisheng.SetActive(false);
        chird_kaifang.SetActive(false);
        UIEventListener.Get(HaveBtn).onClick = ButtonClick;
        UIEventListener.Get(NULLbtn).onClick = ButtonClick;

    }
    ///// <summary>
    ///// 初始化数据
    ///// </summary>
    //public void Init(NnClubMembersResponse resp = null, NnRoomListResponse resp_room = null)
    //{

    //    switch (_currentState)
    //    {
    //        case ButtonState.JiBenState:
    //            JibenInit(resp);
    //            break;
    //        case ButtonState.RuHuiState:
    //            break;
    //        case ButtonState.KaiFangState:
    //            break;
    //    }
    //    ShowNowState();
    //}
    private List<int> users_id = new List<int>();
    /// <summary>
    /// 基本信息初始化
    /// </summary>
    /// <param name="resp"></param>
    public void JibenInit(NnClubMembersResponse resp)
    {

        _id.text = HallModel.Instance._clubid.ToString();
        _clubNum.text = string.Format("{0} / 200", HallModel.Instance._clubNum.ToString());

        for (int i = 0; i < resp.Members.Count; i++)
        {
            if (!users_id.Contains(resp.Members[i].Id))
            {
                GameObject go = GameObject.Instantiate(chird_jibenxinxi);
                go.name = resp.Members[i].Id.ToString();
                go.transform.Find("id").GetComponent<UILabel>().text = resp.Members[i].Id.ToString();
                go.transform.Find("name").GetComponent<UILabel>().text = resp.Members[i].Nickname;
                if (!resp.Members[i].Online)
                {
                    go.transform.Find("isOnline/offline").gameObject.SetActive(true);
                    go.transform.Find("isOnline/online").gameObject.SetActive(false);
                }
                else
                {
                    go.transform.Find("isOnline/offline").gameObject.SetActive(false);
                    go.transform.Find("isOnline/online").gameObject.SetActive(true);
                }
                GameObject chengyuan = go.transform.Find("nohuizhang").gameObject;
                if (i == 0)
                {
                    go.transform.Find("ishuizhang").gameObject.SetActive(true);
                    chengyuan.SetActive(false);
                }
                else
                {
                    go.transform.Find("ishuizhang").gameObject.SetActive(false);
                    chengyuan.SetActive(true);
                }
                UIEventListener.Get(chengyuan).onClick = ButtonClick;
                go.transform.SetParent(grid_jibenxinxi);
                go.transform.localScale = Vector3.one;
                go.transform.localPosition = Vector3.zero;
                go.transform.localRotation = Quaternion.identity;
                go.SetActive(true);
                users_id.Add(resp.Members[i].Id);
                allnum++;
            }

        }
        grid_jibenxinxi.GetComponent<UIGrid>().enabled = true;
        ShowNowState();
    }
    private List<int> club_rooms_num = new List<int>();
    /// <summary>
    /// 开房初始化
    /// </summary>
    /// <param name="resp"></param>
    public void KaifangInit(NnRoomListResponse resp)
    {
        try
        {
            Destroy(grid_kaifang.Find("CreateRoom").gameObject);
            if (resp.Rooms.Count == 0 && resp.Status == STATUS.Success)
            {
                user.logMsg = "没有更多俱乐部房间~";
                GameObject Set = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)(Resources.Load("LogMsg")));
            }
        }
        catch (System.Exception)
        {

            Debug.LogError("---第一次打开加载俱乐部房间");
        }

        for (int i = 0; i < resp.Rooms.Count; i++)
        {
            if (!club_rooms_num.Contains(resp.Rooms[i].RoomId))
            {
                GameObject obj = GameObject.Instantiate(chird_kaifang);
                obj.name = resp.Rooms[i].RoomId.ToString();
                obj.transform.Find("havepeo/roomid").GetComponent<UILabel>().text = resp.Rooms[i].RoomId.ToString();
                obj.transform.Find("havepeo").gameObject.SetActive(true);
                obj.transform.Find("nopeo").gameObject.SetActive(false);
                if (resp.Rooms[i].IsFull)
                {
                    obj.transform.Find("havepeo/renshu").GetComponent<UILabel>().text = "已满";
                }
                else
                {
                    obj.transform.Find("havepeo/renshu").GetComponent<UILabel>().text = resp.Rooms[i].MemberCount + "人";

                }
                obj.transform.Find("havepeo/rule").GetComponent<UILabel>().text = string.Format("国际斗牛、{0}底分", resp.Rooms[i].Score);
                // UIEventListener.Get(obj).onClick = ButtonClick;
                obj.transform.SetParent(grid_kaifang);
                obj.transform.localScale = Vector3.one;
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localRotation = Quaternion.identity;
                obj.SetActive(true);
                club_rooms_num.Add(resp.Rooms[i].RoomId);

            }

        }
        GameObject go = GameObject.Instantiate(chird_kaifang);
        go.name = "CreateRoom";
        go.transform.Find("havepeo").gameObject.SetActive(false);
        go.transform.Find("nopeo").gameObject.SetActive(true);
        UIEventListener.Get(go).onClick = ButtonClick;
        go.transform.SetParent(grid_kaifang);
        go.transform.localScale = Vector3.one;
        go.transform.localPosition = Vector3.zero;
        go.transform.localRotation = Quaternion.identity;
        go.SetActive(true);
        grid_kaifang.GetComponent<UIGrid>().enabled = true;
        ShowNowState();

        KaiFangJieShao.SetActive(false);
        kaifang_ui.transform.Find("Scroll View").gameObject.SetActive(true);
        mmoreBtn.SetActive(true);


    }
    /// <summary>
    /// 开房介绍初始化
    /// </summary>
    /// <param name="resp"></param>
    public void KaiFangJieShaoInit(NnRoomOptionsResponse resp)
    {
        //底注
        Transform dizhu_grid = KaiFangJieShao.transform.Find("Scroll View/Grid");
        GameObject dizhu_chird = KaiFangJieShao.transform.Find("Scroll View/dizhu_Btn").gameObject;
        dizhu_chird.SetActive(false);
        //倍数
        Transform beishu_grid = KaiFangJieShao.transform.Find("Scroll View2/Grid");
        GameObject beishu_chird = KaiFangJieShao.transform.Find("Scroll View2/beishu_Btn").gameObject;
        beishu_chird.SetActive(false);
        try
        {
            for (int i = 0; i < dizhu_grid.childCount; i++)
            {
                Destroy(dizhu_grid.GetChild(i).gameObject);
            }
            for (int j = 0; j < beishu_grid.childCount; j++)
            {
                Destroy(beishu_grid.GetChild(j).gameObject);
            }
        }
        catch (System.Exception)
        {


        }

        for (int i = 0; i < resp.Score.Count; i++)
        {
            GameObject obj = GameObject.Instantiate(dizhu_chird);
            obj.name = "dizhu_Btn";
            obj.transform.SetParent(dizhu_grid);
            obj.transform.localPosition = Vector3.zero;
            obj.transform.localScale = Vector3.one;
            obj.transform.localRotation = Quaternion.identity;
            GameObject go = obj.transform.Find("Background/GameObject").gameObject;
            go.name = resp.Score[i].ToString();
            go.transform.GetComponent<UILabel>().text = resp.Score[i] + "底注";
            UIEventListener.Get(obj).onClick = ButtonClick;
            obj.SetActive(true);
            dizhu_sprites.Add(obj);
            if (i == 0)
            {
                obj.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
            }
        }
        dizhu_grid.GetComponent<UIGrid>().enabled = true;


        foreach (KeyValuePair<int, string> item in resp.Multiple)
        {
            GameObject obj = GameObject.Instantiate(beishu_chird);
            obj.name = "beishu_Btn";
            obj.transform.SetParent(beishu_grid);
            obj.transform.localPosition = Vector3.zero;
            obj.transform.localScale = Vector3.one;
            obj.transform.localRotation = Quaternion.identity;
            obj.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
            GameObject go = obj.transform.Find("Background/GameObject").gameObject;
            go.name = item.Key.ToString();
            go.transform.GetComponent<UILabel>().text = item.Value;
            UIEventListener.Get(obj).onClick = ButtonClick;
            obj.SetActive(true);
        }
        beishu_grid.GetComponent<UIGrid>().enabled = true;

        KaiFangJieShao.SetActive(true);
        kaifang_ui.transform.Find("Scroll View").gameObject.SetActive(false);
        mmoreBtn.SetActive(false);

        NULLbtn.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
        //默认设置
        _isNullRoom = 1;
        for (int i = 1; i < 8; i++)
        {
            current_beishu.Add(i);
        }
        current_dizhu = 5000;
    }

    /// <summary>
    /// 展示当前打开的俱乐部功能界面
    /// </summary>
    public void ShowNowState()
    {
        switch (_currentState)
        {
            case ButtonState.JiBenState:
                jiben_an_btn.SetActive(false);
                ruhuisend_an_btn.SetActive(true);
                kaifang_an_btn.SetActive(true);

                jibenxinxi_ui.SetActive(true);
                ruhuisend_ui.SetActive(false);
                kaifang_ui.SetActive(false);
                grid_jibenxinxi.GetComponent<UIGrid>().enabled = true;
                break;
            case ButtonState.RuHuiState:
                jiben_an_btn.SetActive(true);
                ruhuisend_an_btn.SetActive(false);
                kaifang_an_btn.SetActive(true);

                jibenxinxi_ui.SetActive(false);
                ruhuisend_ui.SetActive(true);
                kaifang_ui.SetActive(false);
                grid_kaifang.GetComponent<UIGrid>().enabled = true;
                break;
            case ButtonState.KaiFangState:
                jiben_an_btn.SetActive(true);
                ruhuisend_an_btn.SetActive(true);
                kaifang_an_btn.SetActive(false);

                jibenxinxi_ui.SetActive(false);
                ruhuisend_ui.SetActive(false);
                kaifang_ui.SetActive(true);
                grid_ruhuisheng.GetComponent<UIGrid>().enabled = true;
                break;
        }
    }
    public void ButtonClick(GameObject go)
    {

        if (go.name == "CloseBtn")
        {
            Destroy(gameObject);
        }
        else if (go.name == "changename")
        {
            //提示修改的规则....
            Debug.LogError("---修改名字---");
        }
        else if (go.name == "jiben_an")
        {
            _currentState = ButtonState.JiBenState;
            //GetMemberRep();
            ShowNowState();
        }
        else if (go.name == "ruhuisend_an")
        {
            _currentState = ButtonState.RuHuiState;
            GetRuHuiReq();
            //ShowNowState();
        }
        else if (go.name == "kaifang_an" || go.name == "mmore")
        {
            _currentState = ButtonState.KaiFangState;
            GetRoomListReq();

        }
        else if (go.name == "more")
        {
            GetMemberRep();
        }
        else if (go.name == "nohuizhang")
        {
            RemoveMemberReq(go);
        }
        else if (go.name == "CreateRoom")
        {
            GetKaiFangReq();
        }
        else if (go.name == "beishu_Btn")
        {

            UISprite _us = go.transform.Find("Background").transform.GetComponent<UISprite>();
            //说明选中了这时候应该删除
            if (_us.spriteName == "xz4")
            {

                go.transform.Find("Background").transform.GetComponent<UISprite>().spriteName = "xz3";
                current_beishu.Remove(int.Parse(go.transform.Find("Background").transform.GetChild(0).name));
            }
            else//说明没选，应该添加
            {
                go.transform.Find("Background").transform.GetComponent<UISprite>().spriteName = "xz4";
                current_beishu.Add(int.Parse(go.transform.Find("Background").transform.GetChild(0).name));
            }

        }
        else if (go.name == "dizhu_Btn")
        {
            //将所有选中关闭
            for (int i = 0; i < dizhu_sprites.Count; i++)
            {
                dizhu_sprites[i].transform.Find("Background").GetComponent<UISprite>().spriteName = "xz3";
            }
            go.transform.Find("Background").transform.GetComponent<UISprite>().spriteName = "xz4";
            current_dizhu = int.Parse(go.transform.Find("Background").transform.GetChild(0).name);
        }
        else if (go.name == "AddRoomBtn")
        {
            AddRoomReq();
        }
        else if (go.name == "jujue")
        {
            //NnHandleMemberRequest req = new NnHandleMemberRequest();
            //req.HandleType = NN_HANDLE_TYPE.Remove;
            //req.UserId = int.Parse(go.transform.Find("id").GetComponent<UILabel>().text);
            //SocketManager sm = SocketManager.Instance;
            //sm.SendProtobuf1(req, MsgId.NnHandleMemberRequest);
            RemoveMemberReq(go);
        }
        else if (go.name == "tongguo")
        {
            PassReq(go);

        }
        else if (go.name == "NULLBtn")
        {
            if (go.transform.Find("Background").GetComponent<UISprite>().spriteName == "xz3")
            {
                go.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
                HaveBtn.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz3";
                _isNullRoom = 1;
            }
            else
            {
                go.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz3";
                HaveBtn.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
                _isNullRoom = 2;

            }

        }
        else if (go.name == "Have_Btn")
        {
            if (go.transform.Find("Background").GetComponent<UISprite>().spriteName == "xz3")
            {
                go.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
                NULLbtn.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz3";
                _isNullRoom = 2;
            }
            else
            {
                go.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz3";
                NULLbtn.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
                _isNullRoom = 1;

            }
        }


    }
    /// <summary>
    /// 请求获取成员信息
    /// </summary>
    private void GetMemberRep()
    {
        NnClubMembersRequest req = new NnClubMembersRequest();
        req.ClubId = HallModel.Instance._clubid;
        _page += 1;
        req.Page = _page;
        Debug.LogError(">>>>>>>>>>>>page" + req.Page);
        SocketManager sm = SocketManager.Instance;
        sm.SendProtobuf1(req, MsgId.NnClubMembersRequest);
    }

    /// <summary>
    /// 请求踢出或者拒绝某个成员
    /// </summary>
    private void RemoveMemberReq(GameObject go)
    {
        NnHandleMemberRequest req = new NnHandleMemberRequest();
        req.HandleType = NN_HANDLE_TYPE.Remove;
        req.UserId = int.Parse(go.transform.parent.name);
        SocketManager sm = SocketManager.Instance;
        sm.SendProtobuf1(req, MsgId.NnHandleMemberRequest);
        Destroy(go.transform.parent.gameObject);
        if (go.name == "jujue")
        {
            grid_ruhuisheng.transform.GetComponent<UIGrid>().enabled = true;
        }
        else
        {
            allnum--;
            grid_jibenxinxi.transform.GetComponent<UIGrid>().enabled = true;
        }

    }
    /// <summary>
    /// 获取房间列表请求
    /// </summary>
    private void GetRoomListReq()
    {
        NnRoomListRequest req = new NnRoomListRequest();
        req.GroundType = NN_GROUND_TYPE.Club;
        _room_page += 1;
        req.Page = _room_page;
        SocketManager sm = SocketManager.Instance;
        sm.SendProtobuf1(req, MsgId.NnRoomListRequest);
    }
    /// <summary>
    /// 获取开房规则请求
    /// </summary>
    public void GetKaiFangReq()
    {
        NnRoomOptionsRequest req = new NnRoomOptionsRequest();
        SocketManager sm = SocketManager.Instance;
        sm.SendProtobuf1(req, MsgId.NnRoomOptionsRequest);
    }
    /// <summary>
    /// 创建房间请求
    /// </summary>
    public void AddRoomReq()
    {
        if (current_beishu.Count != 0 && _isNullRoom > 0 && current_dizhu != 0)
        {
            NnCreateRoomRequest req = new NnCreateRoomRequest();
            req.GroundType = NN_GROUND_TYPE.Club;
            req.Score = current_dizhu;
            for (int i = 0; i < current_beishu.Count; i++)
            {
                req.Multiple.Add(current_beishu[i]);
            }
            if (_isNullRoom == 1)
            {
                req.IsEmpty = true;
            }
            else
            {
                req.IsEmpty = false;
            }
            //Debug.LogError("----------------------->>>>>>>>>>>");
            //Debug.Log("-----IsEmpty-------" + req.IsEmpty);
            //Debug.Log("-----底注-------" + req.Score);
            //Debug.Log("-----几个倍数需求-------" + req.Multiple.Count);
            //Debug.LogError("---------------------->>>>>>>>>>");
            SocketManager sm = SocketManager.Instance;
            sm.SendProtobuf1(req, MsgId.NnCreateRoomRequest);
        }

    }
    void OnDestroy()
    {
        _instance = null;
    }
    public void GetRuHuiReq()
    {
        NnReviewingMemberListRequest req = new NnReviewingMemberListRequest();
        req.ClubId = HallModel.Instance._clubid;
        SocketManager sm = SocketManager.Instance;
        sm.SendProtobuf1(req, MsgId.NnReviewingMemberListRequest);
    }
    //入会申请初始化
    public void RuHuiInit(NnReviewingMemberListResponse resp)
    {
        for (int i = 0; i < grid_ruhuisheng.childCount; i++)
        {
            Destroy(grid_ruhuisheng.GetChild(i).gameObject);
        }
        for (int i = 0; i < resp.Members.Count; i++)
        {
            GameObject go = GameObject.Instantiate(chird_ruhuisheng);

            go.name = resp.Members[i].Id.ToString();
            go.transform.Find("id").GetComponent<UILabel>().text = resp.Members[i].Id.ToString();
            go.transform.Find("name").GetComponent<UILabel>().text = resp.Members[i].Nickname;
            if (!resp.Members[i].Online)
            {
                go.transform.Find("isOnline/offline").gameObject.SetActive(true);
                go.transform.Find("isOnline/online").gameObject.SetActive(false);
            }
            else
            {
                go.transform.Find("isOnline/offline").gameObject.SetActive(false);
                go.transform.Find("isOnline/online").gameObject.SetActive(true);
            }
            go.transform.SetParent(grid_ruhuisheng);
            go.transform.localScale = Vector3.one;
            go.transform.localPosition = Vector3.zero;
            go.transform.localRotation = Quaternion.identity;
            go.SetActive(true);
            UIEventListener.Get(go.transform.Find("jujue").gameObject).onClick = ButtonClick;
            UIEventListener.Get(go.transform.Find("tongguo").gameObject).onClick = ButtonClick;

        }
        grid_ruhuisheng.transform.GetComponent<UIGrid>().enabled = true;
        ShowNowState();
    }
    /// <summary>
    /// 通过请求
    /// </summary>
    public void PassReq(GameObject go)
    {
        NnHandleMemberRequest req = new NnHandleMemberRequest();
        req.HandleType = NN_HANDLE_TYPE.Pass;
        req.UserId = int.Parse(go.transform.parent.name);
        SocketManager sm = SocketManager.Instance;
        sm.SendProtobuf1(req, MsgId.NnHandleMemberRequest);

        Destroy(go.transform.parent.gameObject);
        grid_ruhuisheng.transform.GetComponent<UIGrid>().enabled = true;

        NnClubMembersRequest reqs = new NnClubMembersRequest();
        reqs.ClubId = HallModel.Instance._clubid;
        if (allnum % 10 == 0)
        {
            reqs.Page = _page + 1;
        }
        else
        {
            reqs.Page = _page;
        }

        SocketManager sms = SocketManager.Instance;
        sms.SendProtobuf1(req, MsgId.NnClubListRequest);


    }


}
